import { getSliderValue } from "./displayInfor.js";

export function stopByTrailBuyOrSell() {
  const btn = document.querySelector(".trading_sub > .mantine-1v71nuo");
  console.log(btn, "btn");
  if (btn) {
    const originalHandler = btn.onclick;
    if (originalHandler) {
      btn.onclick = null;
    }
    btn.addEventListener("click", async (event) => {
      const sliderValue = getSliderValue();
      const priceValue = getTrailPriceValue();
      const inputValue = getTrailInput();
      const periodOfValidity = getTrailValidity();
      console.log(
        sliderValue,
        priceValue,
        inputValue,
        periodOfValidity,
        "inputValue"
      );

      console.log("买入按钮被点击");
      event.preventDefault();
      console.log("执行自定义逻辑");
      // chrome.runtime.sendMessage(
      //   {
      //     action: "interceptRequest",
      //     url: "https://www.gate.io/apiw/v2/unified/futures/usdt/contracts",
      //   },
      //   (response) => {
      //     if (chrome.runtime.lastError) {
      //       console.error("消息发送失败：", chrome.runtime.lastError.message);
      //     } else {
      //       console.log("收到响应：", response);
      //     }
      //   }
      // );
    });
  } else {
    console.log("未找到.trading_sub > .mantine-1v71nuo买入按钮节点");
  }
}

function getTrailInput() {
  const loopOrderInputGroup = document.querySelectorAll(
    ".mantine-1i52ecs > input"
  );
  console.log(loopOrderInputGroup, "loopOrderInputGroup");

  if (loopOrderInputGroup) {
    const params = {
      callbackAmplitude: loopOrderInputGroup[0].value,
      activatePrice: loopOrderInputGroup[1].value,
      num: loopOrderInputGroup[2].value,
    };
    return params;
  } else {
    console.log("未找到.mantine-1i52ecs > input节点");
    return null;
  }
}

function getTrailPriceValue() {
  const loopPriceInput = document.querySelector(
    ".trade-input-price-container > input"
  );
  console.log(loopPriceInput, "loopPriceInput");
  return loopPriceInput.value;
}

function getTrailValidity() {
  const validateInput = document.querySelector(
    ".mantine-dcm9q0 > .mantine-3dmfky"
  );
  console.log(validateInput, "validateInput");
  return validateInput.value;
}
